package newPan3d.content.ground.map
{

	import _Pan3D.core.Mathclass;
	
	import _me.Scene_data;
	
	import com.Astart.AcorrsLineGrid;
	import com.Astart.Astart;
	import com.Astart.Grid;
	import com.Astart.Node;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Loader;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Vector3D;
	import flash.net.URLRequest;
	import flash.text.TextField;
	import flash.utils.getTimer;

	public class MapClass
	{


		public function MapClass()
		{
		}
		private static var _canvas:BitmapData;
		private static var _canvasBitmap:Bitmap;

		private static var _grid:Grid;
		private static var _showTime:TextField;
		private static var _randomModul:Boolean=true;
		private static var _acorrs:AcorrsLineGrid;
		private static var _numRow:int=200;
		private static var _numCol:int=200;
		private static var datas:Array;
		private static var _shape:Shape;

		private static var _mapData:BitmapData=new BitmapData(200, 200, false, 0xffffff) //在这初始定义地图尺寸(500*500);
		private static var _haveMapdata:Boolean=false;
		private static var _roadArray:Array;
		[Embed(source="../_File/nijia2/ground/texture/mappic.jpg")]
		private static var pic:Class;

		public static function initData():void
		{
			_mapData.draw(Bitmap(new pic()).bitmapData);
			_drawmap()
			_loadMapPic();
		}
		private static function _loadMapPic():void
		{
			 _loader=new Loader;
			 _loader.load(new URLRequest(Scene_data.fileRoot+"ground/texture/mappic.jpg"));
			 _loader.contentLoaderInfo.addEventListener(Event.COMPLETE,onLoadOK);
		
				
		}
		
		protected static function onLoadOK(event:Event):void
		{
			var loader:Loader = Loader(event.target.loader);
			var image:Bitmap = Bitmap(loader.content);
			_mapData.draw(image.bitmapData);
		//	var kk:BitmapData=new BitmapData(200, 200, false, 0xffffff)
			//_mapData.draw(kk);
			_drawmap();
		}
		private static function _drawmap():void{
			
			datas=[];
			var len:int=(_numRow * _numCol) / 100;
			for(var i:int=0;i<200;i++){
				for(var j:int=0;j<200;j++){
					if(_mapData.getPixel(i,j)<0xcc0000){
						datas.push([i, j])
					}
				}
			}
			init();

		}
		private static function init():void {
			_canvas = new BitmapData(200, 200, false, 0xffffff);
			_grid = new Grid(200, 200,_numRow,_numCol);
			_grid.canvasGrid(_canvas);
			_acorrs = new AcorrsLineGrid(_grid);
			var nodes:Array = _grid.nodes;
			for each(var a:Array in datas) {
				var yn:int = int(a[1]); var xn:int = int(a[0]);
				Node(nodes[yn][xn]).walkAble=false
			}
			_grid.nodes[0][0].walkAble = true;
			_grid.drawUnWalks();

		}
		public static function getFindLoadArr(bx:int, bz:int, ex:int, ez:int):Vector.<Vector3D>
		{
			var backArr:Vector.<Vector3D>=new Vector.<Vector3D>;
			bx=roundPosition(bx);
			bz=roundPosition(bz);
			ex=roundPosition(ex);
			ez=roundPosition(ez);
			
			var hitNodeB:Node = _grid.getNodeByXY(bx, bz) as Node;
			var hitNodeE:Node = _grid.getNodeByXY(ex, ez) as Node;
			
			if (!hitNodeB||!hitNodeB.walkAble) {
				
				//如果开始的点都不可以走，那就找边上最近可以走的;
				hitNodeB=findNextCanWalk(bx,bz);
			}
			if (!hitNodeE||!hitNodeE.walkAble){
				//最后一个点为不可寻走，时做向前碰到就停
				return makeLineToHitArr(bx,bz,ex,ez);
			}
			_grid.startNode =hitNodeB;
			_grid.endNode = hitNodeE;
			var loadArr:Array=findPath();
			if(loadArr){
			
				for(var i:int =0;i<loadArr.length;i++)
				{
					var tempVector3D:Vector3D=new Vector3D(loadArr[i].x,0,loadArr[i].y)
					backArr.push(tempVector3D);
				}
				return backArr;
			}
			return null;
		}
		private static function findNextCanWalk(bx:int,bz:int):Node
		{
			var d:int=0;
			var node:Node;
			while(true){
				d++;
				var hitNodeA:Node = _grid.getNodeByXY(bx+d, bz) as Node;
				var hitNodeB:Node = _grid.getNodeByXY(bx-d, bz) as Node;
				var hitNodeC:Node = _grid.getNodeByXY(bx, bz+d) as Node;
				var hitNodeD:Node = _grid.getNodeByXY(bx, bz-d) as Node;
				if(hitNodeA&&hitNodeA.walkAble){
					node= hitNodeA;
					break;
				}
				if(hitNodeB&&hitNodeB.walkAble){
					node= hitNodeB;
					break;
				}
				if(hitNodeC&&hitNodeC.walkAble){
					node= hitNodeC;
					break;
				}
				if(hitNodeD&&hitNodeD.walkAble){
					node= hitNodeD;
					break;
				}
			}
			
			return node;
		}
		private static function makeLineToHitArr(bx:int, bz:int, ex:int, ez:int):Vector.<Vector3D>
		{
			var d:Number=Mathclass.math_distance(ex, ez, bx, bz)
			var angly:Number=Mathclass.math_angle(ex, ez, bx, bz)
	
			var i:int=0;
			var nextX:Number;
			var nextZ:Number;
			
			var backArr:Vector.<Vector3D>=new Vector.<Vector3D>;
			backArr.push(new Vector3D(bx,0,bz))
			var tempVector3D:Vector3D=new Vector3D(bx,0,bz)
			while(i<d){
				
				var hitNodeB:Node = _grid.getNodeByXY(bx, bz) as Node;
				nextX=bx + i * Math.cos(angly * Math.PI / 180);
				nextZ=bz + i * Math.sin(angly * Math.PI / 180);
				
				var hitNode:Node = _grid.getNodeByXY(nextX, nextZ) as Node;
				
				if(!(hitNode&&hitNode.walkAble)){
					break;
				}
				tempVector3D=new Vector3D(nextX,0,nextZ)
		
			//	trace(i,nextX,nextZ,(hitNode&&hitNode.walkAble))
				
				i++
			}
			backArr.push(tempVector3D);
				
			trace("直到碰到为止，走直线",d,angly);
			
			return backArr;
			
				
			
		}
		
		private static var _path:Array;
		private static var _loader:Loader;
		private static function findPath():Array {
			_grid.canvasGrid(_canvas);
			_grid.nodesRest();
			Astart.ProfectPath = true;
			_path = Astart.findPath(_grid);
			_grid.drawUnWalks();
			if (_path == null|| _path.length==0) {
				trace("NO path!!");
				return null;
			}
			var psArr:Array = _acorrs.getPsnode(_path);

	
			return psArr;
			
		}




		public static function getMapPixel(i:int, j:int):int
		{
			//	trace(_mapData. getPixel(i,j));
			return _roadArray[i][j];
		}


		private static function roundPosition(num:int):int
		{
			// 保证在区间内
			if (num < 0)
			{
				return 0
			}
			if (num >= 199)
			{
				return 199;
			}
			return int(num);
		}

	}
}
